package com.kakarote.admin.common;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class SmsUtil {

    private static final String ACCESS_KEY = "LTAI4G4ww3sp53QKF3HNh6JE";

    private static final String SECRET = "NbT07b1eOdJ8DTetcXlEQPn1nLsC26";

    private static final String JSON_KEY = "Code";
    private static final String JSON_MESSAGE = "OK";

    /**
     * 发送验证短信
     *
     * @param phone phone
     * @param code  验证码
     * @return true为发送成功
     */
    public static boolean sendSms(String phone, String code) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", ACCESS_KEY, SECRET);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "悟空CRM");
        request.putQueryParameter("TemplateCode", "SMS_161891139");
        request.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            JSONObject object = JSON.parseObject(response.getData());
            if (JSON_MESSAGE.equals(object.getString(JSON_KEY))) {
                return true;
            } else {
                log.error("验证码发送失败：{}", object.getString("Message"));
            }
        } catch (ClientException e) {
            log.error("验证码发送失败", e);
        }
        return false;
    }

    /**
     * 发送云平台即将过期通知短信
     *
     * @param phone       手机号
     * @param date        时间
     * @param day         剩余天数
     * @param companyName 企业名称
     */
    public static void sendMessage(String phone, String date, Integer day, String companyName) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", ACCESS_KEY, SECRET);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "悟空CRM");
        request.putQueryParameter("TemplateCode", "SMS_210995338");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("companyName", StrUtil.subSufByLength(companyName, 20));
        jsonObject.put("date", date);
        jsonObject.put("day", day);
        request.putQueryParameter("TemplateParam", jsonObject.toJSONString());
        try {
            CommonResponse response = client.getCommonResponse(request);
            JSONObject object = JSON.parseObject(response.getData());
            if (!JSON_MESSAGE.equals(object.getString(JSON_KEY))) {
                log.error("验证码发送失败：{}", object.getString("Message"));
            }
        } catch (ClientException e) {
            log.error("验证码发送失败", e);
        }
    }
}
